<template>
  <div v-loading="loading" class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <tree-tool :data="company" :is-root="true" @addDeparts="addDeparts" />
        <!-- 树形的静态组织架构 -->
        <!-- data用来展示数据,props用来配置从哪读取  -->
        <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
          <!-- 传入插槽 -->
          <!-- el-tree的插槽内容,会被循环的生成,有多少节点就循环多少次 -->
          <treeTool slot-scope="{data}" :data="data" @editDeparts="editDeparts" @addDeparts="addDeparts" @delDeparts="getDepartments()" />
          <!-- <template slot-scope="{data}">
            <treeTool :data="data" />
          </template> -->

        </el-tree>

      </el-card>
    </div>

    <addDeparts ref="addDeparts" :show-dialog.sync="showDialog" :tree-node="node" @addDepts="getDepartments" />
  </div>
</template>

<script>
import addDeparts from './components/add-departs.vue'
import { tranListToTreeData } from '@/utils'
import { getDepartments } from '@/api/departments'
import treeTool from './components/tree-tool.vue'
export default {
  components: {
    treeTool,
    addDeparts
  },
  data() {
    return {
      defaultProps: {
        // 如果是空的,默认  label:'label children:'children'
        label: 'name' // 表示 从这个属性显示内容
      },
      departs: [],
      company: { },
      showDialog: false, // 显示窗体
      node: {},
      loading: false // 用来控制进度弹层的显示和隐藏

    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      this.loading = true
      const res = await getDepartments()
      this.departs = tranListToTreeData(res.depts, '')
      this.company = {
        name: res.companyName,
        manager: '负责人',
        id: ''
      }
      // await总是会等到后面的promise进行reslove
      await new Promise(resolve => setTimeout(() => resolve(), 500))
      this.loading = false
    },
    addDeparts(node) {
      this.node = node
      this.showDialog = true
      // 因为node是当前的点击的部门， 此时这个部门应该记录下来,
    },
    editDeparts(node) {
      this.node = node
      this.showDialog = true
      this.$refs.addDeparts.getDepartmentsDetail(node.id)
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
